/**
 * Created by will on 2017/9/27.
 */
$(function () {
    layui.use(['laypage', 'layer','table','laydate'], function() {
        var laypage = layui.laypage
            , layer = layui.layer
            ,table = layui.table
            ,laydate=layui.laydate;
        laypage.render({
            elem: 'reconciliation-paging'
            , count: 100
            , theme: '#45A2FF'
            , groups: 4
            ,prev: '<em><</em>'
            ,next: '<em>></em>'
            , layout: ['prev', 'page', 'next', 'limit' ,'skip']
            , jump: function (obj) {
                console.log(obj)
            }

        });
        //日期控件
        laydate.render({
            elem: '#date1'
            ,type:'datetime'
            ,theme: '#45a2ff'
        });
        laydate.render({
            elem: '#date2'
            ,type:'datetime'
            ,theme: '#45a2ff'
        });
        laydate.render({
            elem: '#date3'
            ,type:'datetime'
            ,theme: '#45a2ff'
        });
        laydate.render({
            elem: '#date4'
            ,type:'datetime'
            ,theme: '#45a2ff'
        });
        laydate.render({
            elem: '#date5'
            ,type:'datetime'
            ,theme: '#45a2ff'
        });
        laydate.render({
            elem: '#date6'
            ,type:'datetime'
            ,theme: '#45a2ff'
        });
        laydate.render({
            elem: '#date7'
            ,type:'datetime'
            ,theme: '#45a2ff'
        });
        laydate.render({
            elem: '#date8'
            ,type:'datetime'
            ,theme: '#45a2ff'
        });
        laydate.render({
            elem: '#date9'
            ,type:'datetime'
            ,theme: '#45a2ff'
        });
        laydate.render({
            elem: '#date10'
            ,type:'datetime'
            ,theme: '#45a2ff'
        });
//转换静态表格
        table.init('demo', {
            height: 315 //设置高度
            //支持所有基础参数
        });
    });
//    查看流水弹窗
   /* $(".look-water-detail").click(function(){
        $(".money-water").show(500);
    })
    $("#money-water-hide").click(function(){
        $(".money-water").hide(500);
    })*/
    //    查看流水按钮弹出层
    //触发事件
    var active = {
        offset: function(othis){
            var type = othis.data('type')
                ,text = othis.text();

            layer.open({
                type: 1
                ,title:false
                ,offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
                ,id: 'layerDemo'+type //防止重复弹出
                ,area:["1400px","740px"]
                ,content: $('.money-water')
                ,closeBtn: true
                ,btnAlign: 'c' //按钮居中
                ,shade: 0.8 //不显示遮罩
                ,yes: function(){
                    layer.closeAll();
                }
            });
        }
    };

    $('.look-water-detail .layui-btn').on('click', function(){
        var othis = $(this), method = othis.data('method');
        active[method] ? active[method].call(this, othis) : '';
    });

//    导出报表
    var idTmr;
    function  getExplorer() {
        var explorer = window.navigator.userAgent ;
        //ie
        if (explorer.indexOf("MSIE") >= 0) {
            return 'ie';
        }
        //firefox
        else if (explorer.indexOf("Firefox") >= 0) {
            return 'Firefox';
        }
        //Chrome
        else if(explorer.indexOf("Chrome") >= 0){
            return 'Chrome';
        }
        //Opera
        else if(explorer.indexOf("Opera") >= 0){
            return 'Opera';
        }
        //Safari
        else if(explorer.indexOf("Safari") >= 0){
            return 'Safari';
        }
    }
    function method5(tableid) {
        if(getExplorer()=='ie')
        {
            var curTbl = document.getElementById(tableid);
            var oXL = new ActiveXObject("Excel.Application");
            var oWB = oXL.Workbooks.Add();
            var xlsheet = oWB.Worksheets(1);
            var sel = document.body.createTextRange();
            sel.moveToElementText(curTbl);
            sel.select();
            sel.execCommand("Copy");
            xlsheet.Paste();
            oXL.Visible = true;

            try {
                var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
            } catch (e) {
                print("Nested catch caught " + e);
            } finally {
                oWB.SaveAs(fname);
                oWB.Close(savechanges = false);
                oXL.Quit();
                oXL = null;
                idTmr = window.setInterval("Cleanup();", 1);
            }

        }
        else
        {
            tableToExcel(tableid)
        }
    }
    function Cleanup() {
        window.clearInterval(idTmr);
        CollectGarbage();
    }
    var tableToExcel = (function() {
        var uri = 'data:application/vnd.ms-excel;base64,',
            template = '<html><head><meta charset="UTF-8"></head><body><table>{table}</table></body></html>',
            base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) },
            format = function(s, c) {
                return s.replace(/{(\w+)}/g,
                    function(m, p) { return c[p]; }) }
        return function(table, name) {
            if (!table.nodeType) table = document.getElementById(table)
            var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
            window.location.href = uri + base64(format(template, ctx))
        }
    })()


})